/************************************************************************
                                                                     
   Xceed Ultimate ListBox for Silverlight                                                                                                                                            
   Copyright (C) 2010 Xceed Software Inc.    
                                                                     
   This program is provided to you under the terms of the GNU General Public  
   License version 2 as published by the Free Software Foundation. 
        
   This program is distributed in the hope that it will be useful, but
   WITHOUT ANY WARRANTY, without even the implied warranty of 
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   General Public License for more details.

   You should have received a copy of the GNU General Public License along 
   with this program, if not, write to the Free Software Foundation, Inc., 
   51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.

   This program can be provided to you by Xceed Software Inc. under a
   proprietary commercial license agreement for use in non-Open Source
   projects. Visit Xceed.com to find the commercial edition of 
   Xceed Ultimate ListBox for Silverlight.                                    
                                                                      
 **********************************************************************/

using System;
using System.Collections.Generic;
using System.Windows;
using System.Windows.Media;
using Xceed.Silverlight.Utils;
using Xceed.Silverlight.ListBox.PositioningFunction;
using System.Collections.ObjectModel;
using System.Collections;

namespace Xceed.Silverlight.ListBox
{
  public sealed class PathView : View
  {
    #region PathConfiguration Property

    public static readonly DependencyProperty PathConfigurationProperty = DependencyProperty.Register(
      "PathConfiguration",
      typeof( PathConfiguration ),
      typeof( PathView ),
      new PropertyMetadata( null, new PropertyChangedCallback( PathView.OnPathConfigurationChanged ) ) );

    public PathConfiguration PathConfiguration
    {
      get
      {
        return ( PathConfiguration )this.GetValue( PathView.PathConfigurationProperty );
      }
      set
      {
        this.SetValue( PathView.PathConfigurationProperty, value );
      }
    }

    private static void OnPathConfigurationChanged( object sender, DependencyPropertyChangedEventArgs e )
    {
      PathView pathView = sender as PathView;

      if( pathView.IsInitializing )
        return;

      PathConfiguration newPathConfiguration = e.NewValue as PathConfiguration;

      if( newPathConfiguration == null )
        throw new ArgumentNullException( "PathConfiguration cannot be null." );


      PathConfiguration oldPathConfiguration = e.OldValue as PathConfiguration;

      if( oldPathConfiguration != null )
      {
        oldPathConfiguration.PositioningFunctionChanged -= new EventHandler( pathView.PathConfigurationPositionFunctionChanged );
        oldPathConfiguration.ScalingFunctionChanged -= new EventHandler( pathView.PathConfigurationScalingFunctionChanged );
        oldPathConfiguration.ZIndexFunctionChanged -= new EventHandler( pathView.PathConfigurationZIndexFunctionChanged );
      }

      newPathConfiguration.PositioningFunctionChanged += new EventHandler( pathView.PathConfigurationPositionFunctionChanged );
      newPathConfiguration.ScalingFunctionChanged += new EventHandler( pathView.PathConfigurationScalingFunctionChanged );
      newPathConfiguration.ZIndexFunctionChanged += new EventHandler( pathView.PathConfigurationZIndexFunctionChanged );

      pathView.UpdateFunctionsFromPathConfiguration();
    }

    private void PathConfigurationPositionFunctionChanged( object sender, EventArgs e )
    {
      PathConfiguration pathConfiguration = ( PathConfiguration )sender;

      this.PositioningFunction = pathConfiguration.PositioningFunction;
    }

    private void PathConfigurationScalingFunctionChanged( object sender, EventArgs e )
    {
      PathConfiguration pathConfiguration = ( PathConfiguration )sender;

      this.ScalingFunction = pathConfiguration.ScalingFunction;
    }

    private void PathConfigurationZIndexFunctionChanged( object sender, EventArgs e )
    {
      PathConfiguration pathConfiguration = ( PathConfiguration )sender;

      this.ZIndexFunction = pathConfiguration.ZIndexFunction;
    }

    private void UpdateFunctionsFromPathConfiguration()
    {
      this.PositioningFunction = this.PathConfiguration.PositioningFunction;
      this.ScalingFunction = this.PathConfiguration.ScalingFunction;
      this.ZIndexFunction = this.PathConfiguration.ZIndexFunction;
    }

    #endregion


    #region BringLastItemToStartingPoint Internal Property

    internal override bool BringLastItemToStartingPoint
    {
      get
      {
        return ( this.PathConfiguration.StartOffset > 0.0d );
      }
    }

    #endregion


    #region ScalingFunction Internal Property

    internal IScalingFunction ScalingFunction
    {
      get;
      set;
    }

    #endregion

    #region ZIndexFunction Private Property

    private IZIndexFunction ZIndexFunction
    {
      get;
      set;
    }

    #endregion

    #region OpacityFunction Internal Property

    internal IOpacityFunction OpacityFunction
    {
      get;
      set;
    }

    #endregion


    #region AllowStickyHeaders Internal Read-Only Property

    internal override bool AllowStickyHeaders
    {
      get
      {
        return false;
      }
    }

    #endregion

    internal override IScalingFunction GetScalingFunction()
    {
      return this.ScalingFunction;
    }

    internal override IOpacityFunction GetOpacityFunction()
    {
      return this.OpacityFunction;
    }

    internal override IZIndexFunction GetZIndexFunction()
    {
      return this.ZIndexFunction;
    }

    internal override double GetConfigurationDistanceUsedForExtent( ListBoxContainerConfigurationBase configuration )
    {
      return configuration.PrepareSize.Height;
    }

    internal override void EndInitCore()
    {
      if( this.PathConfiguration == null )
        throw new ListBoxException( "PathConfiguration cannot be null on the PathView." );

      base.EndInitCore();

      this.PathConfiguration.PositioningFunctionChanged += new EventHandler( this.PathConfigurationPositionFunctionChanged );
      this.PathConfiguration.ScalingFunctionChanged += new EventHandler( this.PathConfigurationScalingFunctionChanged );
      this.PathConfiguration.ZIndexFunctionChanged += new EventHandler( this.PathConfigurationZIndexFunctionChanged );

      this.UpdateFunctionsFromPathConfiguration();
    }

    #region Private Fields

    private PathGeometryHelper m_pathGeometryHelper = new PathGeometryHelper();

    #endregion
  }
}
